<?php

namespace backend\controllers;

use Yii;
use backend\models\Users;
use common\models\User;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * UsersController implements the CRUD actions for Users model.
 */
class UsersController extends PublicController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login'],
                        'allow' => true,
                    ],
                    [//认证过的用户允许访问的action
                        'actions' => ['index', 'view', 'create', 'update', 'forbidden'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
//                    'create' => ['post'],
//                    'update' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Users models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Users::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new Users model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Users();

        if (Yii::$app->request->isPost) {
            $data = Yii::$app->request->post();
            $user = new User();
            $user->generateAuthKey();
            $user->setPassword($data['Users']['password']);
            $model->auth_key = $user->auth_key;
            $model->password_hash = $user->password_hash;
            $model->username = $data['Users']['username'];
            $model->status = 10;
            $model->created_at = time();
            $model->updated_at = time();
            $model->limitStr = $data['Users']['limitStr'];
            // print_r($model);exit;
            $model->save(false);
            return $this->redirect(['index']);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Users model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if (Yii::$app->request->isPost) {
            $data = Yii::$app->request->post();
            $user = new User();
            $user->generateAuthKey();
            $user->setPassword($data['Users']['password']);
            $model->auth_key = $user->auth_key;
            $model->password_hash = $user->password_hash;
            $model->username = $data['Users']['username'];
            $model->status = 10;
            $model->updated_at = time();
            $model->limitStr = $data['Users']['limitStr'];
            // print_r($model);exit;
            $model->save(false);
            return $this->redirect(['index']);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    public function actionForbidden($id){
        $model = $this->findModel($id);
        if($model->status == 10){
            $model->status = 11;
        }elseif($model->status == 11){
            $model->status = 10;
        }
        $model->save(false);
        return $this->redirect(['index']);
    }

    /**
     * Finds the Users model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Users the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Users::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
